package com;



import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mail.SendMail;

import com.connection.jdbconnection;

public class SetVerified extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
		Connection conn;
		Statement st;
		String dburl;
		ResultSet rs ;
    
    


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
					
					 HttpSession session = request.getSession();
					
					 String state=request.getParameter("verify");
					 System.out.println(state);
					 String state1=request.getParameter("delete");
					 System.out.println(state1);
					 
					 if(state!=null) 
					 {
					String status[]=request.getParameterValues("state");
					if(status !=null){
						try {
							jdbconnection db=new jdbconnection();
							st=db.jdbConnection();
							int b=0;
						for (int i = 0; i < status.length; i++) {
							System.out.println(status[i]);
						
							String QString="update register set status = 1 where id='"+status[i]+"'";
							String sql="select * from register where id='"+status[i]+"'"; 
							System.out.println(sql);
						ResultSet	rs1=st.executeQuery(sql);
							
									if(rs1.next())
									{
										String uname=rs1.getString("userName");
				                        String password=rs1.getString("password");
				                        String email=rs1.getString("email");
				                        System.out.println(uname);
				                        System.out.println(email);
										
										
				                        String sub="Verified Successfully in your ID,So you are login the site in any time.";
				        				String msg="Dear "+uname+" your Password is "+password+". ";
				        				
				                         
										SendMail mail=new SendMail();
										mail.sendMessageToMail(email,sub ,msg );
									}
							System.out.println("Record"+b);						
							b=st.executeUpdate(QString);
				           System.out.println(b);
						}
					
						if(b>0)
						response.sendRedirect("ListUnverifiedUser.jsp");
							
						}catch (Exception e) {
							// TODO: handle exception
							e.printStackTrace();
						}
					}
					
					 }
					 else if(state1!=null)
					 {
							String status[]=request.getParameterValues("state");
							if(status !=null){
								try {
									jdbconnection db=new jdbconnection();
									st=db.jdbConnection();
									int b=0;
								for (int i = 0; i < status.length; i++) {
									System.out.println(status[i]);
								
									
									String QString="Delete from register where id ='"+ status[i]+ "'";
									System.out.println(QString);
								b=st.executeUpdate(QString);
									
								}
							
								if(b>0)
								response.sendRedirect("ListUnverifiedUser.jsp");
									
								}catch (Exception e) {
									// TODO: handle exception
									e.printStackTrace();
								}
							}
					 }
					/*try {
						st=conn.createStatement();
						} 
					catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						}
							for (int i = 0; i < status.length; i++) {
									try {
											st.executeUpdate("update register set status = 1 where status='"+status[i]+"'");
											System.out.println("ok");
										}
									catch (SQLException e) {
											// TODO Auto-generated catch block
											e.printStackTrace();
												}
										
							}	
							
						
								
							session.setAttribute("error", new Object());
			    			response.sendRedirect("ListUnverifiedUser.jsp");
							
							*/
						}

				}
